﻿using System;
using System.Data.Entity;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers.HomeController
{
    public class HomeController : Controller
    {
        DB1Entities model = new DB1Entities();

        //int pageSize = 20;//每页显示记录数

        // GET: Home
        public ActionResult Index(int id = 1)
        {
            ViewBag.Title = "主页";
            ViewBag.PageIndex = id;//返回当前是第几页
            return View();
        }

        public ActionResult NewForm()
        {
            ViewBag.Title = "添加数据";
            return View();
        }

        public async Task<ActionResult> Json1()
        {
            var q1 = from a in model.Table1
                     orderby a.ID//使用Skip方法时一定要排序
                     select a;
            //var data = q1.Skip(pageSize * (id - 1)).Take(pageSize);
            //int count = q1.Count();//获取记录总数
            //double pageCount = Math.Ceiling((double)count / pageSize);
            //return Json(data);
            return Json(await q1.ToListAsync());
        }

        //获取总页数
        //public double PageCount()
        //{
        //    var q1 = from a in model.Table1
        //             orderby a.ID//使用Skip方法时一定要排序
        //             select a;
        //    int count = q1.Count();//获取记录总数
        //    double pageCount = Math.Ceiling((double)count / pageSize);
        //    return pageCount;
        //}

        [HttpPost]
        public async Task<int> AddData(string pName, string sex, string s_birthday)
        {
            string dateFormat = "yyyy-MM-dd";//日期控件格式化模板
            var provider = CultureInfo.CurrentCulture;

            DateTime date_birthday = new DateTime();
            bool dateConvertResult = DateTime.TryParseExact(s_birthday, dateFormat, provider, DateTimeStyles.None, out date_birthday);//将输入的日期字符串转换成DateTime

            bool inputCheck = pName.Trim().Length > 0 && sex.Length > 0 && dateConvertResult;//验证输入数据格式是否正确

            //若前端必填项验证失效，则在服务器端验证；若任意一项有空值则不采取任何操作

            if (inputCheck)
            {
                var t1 = new Table1
                {
                    Name = pName,
                    Sex = sex,
                    Birthday = date_birthday,
                };
                model.Table1.Add(t1);//添加数据
                return await model.SaveChangesAsync();
            }
            else
            {
                return 0;
            }
        }

        [HttpPost]
        public async Task<ActionResult> DeleteData()
        {
            string selectId = Request["data_id"];
            string[] s_id = selectId.Split(',');
            int[] targetID = Array.ConvertAll(s_id, s => int.Parse(s));//数组类型转换
            //查询出要删除的数据
            var query = from t1 in model.Table1
                        where targetID.Contains(t1.ID)
                        select t1;
            //循环逐行删除
            foreach (var r in query)
            {
                model.Table1.Remove(r);
            }
            await model.SaveChangesAsync();
            return Redirect("/");
        }
    }
}